#include "ULUI_LookupLatest.h"
#include "UL_Global.h"
#include "ULGfx_History.h"

#define LATESTN_LOOKUP_START_X	100
#define LATESTN_LOOKUP_START_Y	100
#define SERIALS_PER_SCREEN	8

ULUI_LookupLatest ULUI_LookupLatest::latestNLookup;

enum HistoryLookupIDs{
	RANGE_BOX,
	OK,
	EXIT	
};	

static char *menuTitles[] = {
	"                                ",
	"确定",
	"返回",
};

#define FIXED_GAP	20

void ULUI_LookupLatest::Init()
{
	int i;
	ULUI_Menu menu;

	AttachEventHandler(this);

	pad.Reset();
	pad.EnableSolidBg(true);
	menu.SetFont(UL_Global::UnionLottoFont());
	pad.SetFont(UL_Global::UnionLottoFont());
	selectedMenu = -1;

	for (i = 0; i <= EXIT; i++){
		menu.SetID(i);
		menu.SetTitle(menuTitles[i]);
		menus.AddMenu(menu);
	}

	//Find menu's X is after tip
	menu.SetTitle("                                ");
	menu.GetTitleSize(tipWidth, tipHeight);
	menus.SetFirstMenuXY(VSDL_Point(LATESTN_LOOKUP_START_X + tipWidth, LATESTN_LOOKUP_START_Y + tipHeight));
	pad.SetStartXY(VSDL_Point(LATESTN_LOOKUP_START_X + tipWidth * 2 + FIXED_GAP, LATESTN_LOOKUP_START_Y));
	selectedBox = RANGE_BOX;
	nSerials = serialOffset = 0;
}

void ULUI_LookupLatest::CleanUp()
{
	pad.Reset();
	menus.Reset();
}

void ULUI_LookupLatest::Pause()
{
}

void ULUI_LookupLatest::Resume()
{
	selectedMenu = -1;
}

void ULUI_LookupLatest::Update(VSDL_GameEngine *game)
{
	if (selectedMenu == -1){
		return ;
	}

	switch (selectedMenu){
		case OK:
			if (pad.GetString() != NULL){
				nSerials = strtoul(pad.GetString(), NULL, 10);
			}else{
				nSerials = 0;
			}
			break;
		case EXIT:
			game->PopState();
			break;
	}
}

void ULUI_LookupLatest::Draw(VSDL_GameEngine *game)
{
#define BALLS_PER_ROW	4
	VSDL_RGB rgbNormal(100, 100, 100);
	VSDL_RGB rgbSelected(250, 100, 100);
	VSDL_RGB rgb;
	char *str;
	VSDL_Point currentXY(LATESTN_LOOKUP_START_X, LATESTN_LOOKUP_START_Y);
	SDL_Surface *destSurf = game->GetSurface();
	ULG_History *ulgHistory = ULG_History::Instance();;
	int serialNo;
	int historyY = LATESTN_LOOKUP_START_Y + (EXIT + 2) * tipHeight;


	ulgHistory->SetStartXY(VSDL_Point(LATESTN_LOOKUP_START_X, historyY));

	UL_Global::UnionLottoShowBg();
	UL_Global::UnionLottoShowTitle();
	menus.Show(destSurf);
	//Draw history data background
	SPG_RectFilled(destSurf, LATESTN_LOOKUP_START_X, historyY, destSurf->w, destSurf->h, rgbNormal.GetRGB32(destSurf));
	//Show tips
	UL_Global::UnionLottoShowTip("输入期数进行查询", currentXY);
	currentXY.y += tipHeight;
	UL_Global::UnionLottoShowTip("期数:(例:16)", currentXY);
	
	str = pad.GetString();

	rgb = rgbSelected;

	if (str != NULL){
		UL_Global::UnionLottoShowTip(str, VSDL_Point(currentXY.x + tipWidth, currentXY.y));
	}

	SPG_Rect(destSurf, currentXY.x + tipWidth, currentXY.y, currentXY.x + tipWidth * 2, currentXY.y + tipHeight, rgb.GetRGB32(destSurf));
	rgb = rgbNormal;

	//Draw input pad
	pad.Show(destSurf);
	//Draw spliting line
	historyY--;
	SPG_Line(destSurf, currentXY.x, historyY, destSurf->w, historyY, rgbNormal.GetRGB32(destSurf));
	//Show lates N
	if (nSerials == 0){
		return ;
	}

	ULC_Serial *startSerial = ulgHistory->GetLatestSerials(nSerials);

	ulgHistory->ShowRange(destSurf, startSerial->sn + serialOffset , SERIALS_PER_SCREEN);

	
	char buf[128];
	list<int> retList;
	list<int>::iterator iter;
	int count = 0;
	int tipStartX;

	tipStartX = (destSurf->w >> 1) + FIXED_GAP + (tipWidth >> 1);
	currentXY.x = tipStartX;
	currentXY.y = historyY;
	sprintf(buf, "最近%d期中出现的蓝球:", nSerials);
	UL_Global::UnionLottoShowTip(buf, currentXY);
	currentXY.y += tipHeight;

	ulgHistory->GetLatestNSerialsBlues(retList, nSerials);

	for (iter = retList.begin(); iter != retList.end(); iter++){
		sprintf(buf, "%02d  ", *iter);
		UL_Global::UnionLottoShowTip(buf, currentXY);
		currentXY.x += (tipWidth >> 2);
		count++;

		if (count == BALLS_PER_ROW){
			count = 0;
			currentXY.x = tipStartX;
			currentXY.y += tipHeight;
		}	
	}	

}

ULUI_LookupLatest *ULUI_LookupLatest::Instance()
{
	return &latestNLookup;
}


void __HANDLE_EVENT ULUI_LookupLatest::OnKeyDown(SDLKey keysym, unsigned char *keyStates, void *arg)
{
	VSDL_GameEngine *game = reinterpret_cast<VSDL_GameEngine *>(arg);

	selectedMenu = -1;
	//Basic
	if (keyStates[SDLK_ESCAPE]){
		game->PopState();
	}

	//Special cases
	if (keyStates[SDLK_RETURN]){
		selectedMenu = OK;
		return ;
	}

	if (nSerials > SERIALS_PER_SCREEN){
		if (keyStates[SDLK_PAGEUP]){
			if (serialOffset > 0){
				serialOffset--;
			}
		}

		if (keyStates[SDLK_PAGEDOWN]){
			serialOffset++;

			if (serialOffset + SERIALS_PER_SCREEN >= nSerials){
				serialOffset = nSerials - SERIALS_PER_SCREEN;
			}
		}
	}

	pad.PushKeysym(keysym);
}

void __HANDLE_EVENT ULUI_LookupLatest::OnMouseMove(int button, unsigned char buttonState, int absX, int absY, int relX, int relY, void *arg)
{
	menus.UpdateMenuList(absX, absY);
	pad.UpdateStatus(absX, absY);
}

void __HANDLE_EVENT ULUI_LookupLatest::OnMouseDown(int button, unsigned char buttonState, int absX, int absY, void *arg)
{
	int i;
	VSDL_GameEngine *game = reinterpret_cast<VSDL_GameEngine *>(arg);

	selectedMenu = -1;

	for (i = 0; i <= EXIT; i++){
		ULUI_Menu &menu = menus.GetMenu(i);

		if (menu.IsXYInRect(absX, absY)){
			selectedMenu = i;
			break;
		}
	}

	pad.OnMouseDown(absX, absY);
	//Special case for WHEEL
	if (button == SDL_BUTTON_WHEELUP){
		if (serialOffset > 0){
			serialOffset--;
		}
	}

	if (button == SDL_BUTTON_WHEELDOWN){
		serialOffset++;

		if (serialOffset + SERIALS_PER_SCREEN >= nSerials){
			serialOffset = nSerials - SERIALS_PER_SCREEN;
		}
	}

}

void __HANDLE_EVENT ULUI_LookupLatest::OnQuit(void *arg)
{
	VSDL_GameEngine *game = reinterpret_cast<VSDL_GameEngine *>(arg);

	game->Quit();
}
